package com.yohann.vod.controller;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.vod.model.v20170321.DeleteVideoRequest;
import com.aliyuncs.vod.model.v20170321.DeleteVideoResponse;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthRequest;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthResponse;
import com.yohann.commonutils.result.R;
import com.yohann.vod.service.VodService;
import com.yohann.vod.utils.AliyunVodSDKUtils;
import com.yohann.vod.utils.ConstantVod;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * <p>
 *
 * </p>
 *
 * @author Yohann
 * @since 2020/8/10
 */
@RestController
@RequestMapping("/vod-service/video")
public class VodController {
    @Autowired
    private VodService vodService;

    /**
     * 上传视频
     * @param file
     * @return
     */
    @PostMapping("upload")
    public R uploadVideo(MultipartFile file){
        String id = vodService.uploadVideo(file);

        return R.ok().data("id",id);
    }

    /**
     * 删除阿里云中的视频
     * @param id
     * @return
     */
    @DeleteMapping("delete/{id}")
    public R deleteVideo(@PathVariable String id){
        vodService.removeVideo(id);
        return R.ok();
    }

    /**
     * 批量删除视频
     * @param videoIdList
     * @return
     */
    @DeleteMapping("delete-batch")
    public R deleteBatch(@RequestParam("videoIdList") List<String> videoIdList){
        vodService.removeVideoList(videoIdList);
        return R.ok();
    }

    /**
     * 获取播放授权
     * @param id
     * @return
     */
    @GetMapping("getPlayAuth/{id}")
    public R getPlayAuth(@PathVariable String id){
        String playAuth = null;
        try {
            DefaultAcsClient client = AliyunVodSDKUtils.initVodClient(ConstantVod.ACCESS_KEY_ID,ConstantVod.ACCESS_KEY_SECRET);
            GetVideoPlayAuthRequest request = new GetVideoPlayAuthRequest();
            request.setVideoId(id);

            GetVideoPlayAuthResponse response = client.getAcsResponse(request);
            playAuth = response.getPlayAuth();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return R.ok().data("playAuth",playAuth);
    }
}